package com.cn.yunhe.rentcars.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.model.BusiLog;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 日志页面控制器
 * 
 * @author 朱钦明
 *
 */
@RestController
public class BusiLogController {
	@Autowired
	private BusiLogMapper busiLogMapper;
	
	/**
	 * 跳转到busiloglist页面
	 * @return ModelAndView对象 跳转到busiloglist页面
	 */
	@RequestMapping(value="/busiloglist",method=RequestMethod.GET)
	public ModelAndView busiLogList(){
		return new ModelAndView("busiloglist");
	}
	
	/**
	 * 根据指定日志编号查询指定日志
	 * @param id 传入的日志编号
	 * @return 返回一个视图对象跳转到busilogget页面
	 */
	@RequestMapping(value="/busilogget",method=RequestMethod.GET)
	public ModelAndView busiLogGet(@RequestParam String id){
		BusiLog busilog = busiLogMapper.getBusiLogById(id);
		
		/**
		 * 把日期格式转换成字符串
		 */
		Date date = busilog.getAddTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(date);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("busilog", busilog);
		map.put("dateStr", dateStr);
		return new ModelAndView("busilogget",map);
	}
	
	/**
	 * 查询所有日志信息
	 * @return Map集合 包含返回给前台页面的所有信息
	 * @param datemin 开始日期
	 * @param datemax 结束日期
	 * @param busiName 业务名称
	 * @param busiType 业务类型
	 * @param draw 第几次访问
	 * @param start 开始的记录数
	 * @param length 一共有几条记录
	 * 
	 */
	@RequestMapping(value="/busilogs",method=RequestMethod.GET)
	public Map<String,Object> getBusiLogList(@RequestParam String datemin,@RequestParam String datemax,@RequestParam String busiName,@RequestParam String busiType,
			@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		
		Map<String,Object> paramMaps = new HashMap<String,Object>();
		/**
		 * 把日期格式化成date类型
		 */
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		Date minDate = null;
		Date maxDate = null;
		if(datemin!=null && !"".equals(datemin)){
			try {
				minDate = sdf.parse(datemin);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(datemax!=null && !"".equals(datemax)){
			try {
				maxDate = sdf.parse(datemax);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		paramMaps.put("minDate", minDate);
		paramMaps.put("maxDate", maxDate);
		paramMaps.put("busiName", busiName);
		paramMaps.put("busiType", busiType);
		
		int pageNum = (start/length)+1;
		int pageSize = length;
		PageHelper.startPage(pageNum, pageSize);
		List<BusiLog> list = busiLogMapper.getBusiLogs(paramMaps);
		
		PageInfo<BusiLog> page = new PageInfo<BusiLog>(list);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("draw", draw);
		map.put("data", page.getList());
		map.put("recordsTotal", page.getTotal());
	    map.put("recordsFiltered", page.getTotal());
		return map;
	}
}
